//
//  FavoriteViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FavoriteViewController.h"
#import "Header.h"
#import "FMDBTool.h"
#import "UIImageView+WebCache.h"


#import "AGLChannel.h"
#import "AGLChannelCollectionCell.h"
#import "VideoDetailViewController.h"
@interface FavoriteViewController ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource
>

@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)BaseCollectionView *contentCollection;
@property (nonatomic, assign)BOOL isFavorite;

@end

@implementation FavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"赞过的视频";
    [[FMDBTool shareFMDBTool] openFavoriteDB];
    [[FMDBTool shareFMDBTool] createFavoriteTable];
    self.array = [[FMDBTool shareFMDBTool] selectFavorite];
    
    if (self.array.count == 0) {
        [self createView];
    }else {
        [self createTableView];
    }
    
}


- (void)createTableView {
    UICollectionViewFlowLayout *contentLayout = [[UICollectionViewFlowLayout alloc] init];
    contentLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 15) / 2, SCREEN_HEIGHT / 2.5);
    contentLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    contentLayout.minimumLineSpacing = 5;
    contentLayout.minimumInteritemSpacing = 5;
    contentLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 创建collection
    self.contentCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:contentLayout];
    [self.view addSubview:self.contentCollection];
    //  设置代理
    self.contentCollection.delegate = self;
    self.contentCollection.dataSource = self;
    // 注册
    [self.contentCollection registerClass:[AGLChannelCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([AGLChannelCollectionCell class])];
    
  

}


- (void)createView {
    BaseLabel *label = [[BaseLabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH, 20)];
    [self.view addSubview:label];
    label.text = @"赞过的视频会显示在这里哦~";
    label.textAlignment = NSTextAlignmentCenter;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AGLChannelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([AGLChannelCollectionCell class]) forIndexPath:indexPath];
    AGLChannel *channel = self.array[indexPath.row];
    NSLog(@"%@",channel.picbase);
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",channel.picbase,channel.picm]]];
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:channel.icon]];
    cell.nickLabel.text = channel.nick;
    cell.ftLabel.text = channel.ft;
    cell.lcntLabel.text = [NSString stringWithFormat:@"%@",channel.lcnt];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AGLChannel *channel = self.array[indexPath.row];
    VideoDetailViewController *detailVC = [[VideoDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://gslb.miaopai.com/stream/%@.mp4", channel.scid]];
    detailVC.scid = channel.scid;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
